package cn.trigram.controller;

import cn.trigram.file.PathUtil;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@RequestMapping("/filepath")
@Controller
public class FilePathMvcController {

  @GetMapping("/thread_path")
  @ResponseBody
  public String getThreadCurrentPath() {

    URL resource = PathUtil.getDeployPath();
    return resource.getPath();
  }

  @GetMapping("/nio_path")
  @ResponseBody
  public String getNioPath() throws URISyntaxException {

    URL url = Thread.currentThread().getContextClassLoader().getResource("");
    Path path = Paths.get(url.toURI());
    return path.resolve("../../..").normalize().toAbsolutePath().toString();
  }

  @GetMapping("/index")
  public String index(Model model) {

    model.addAttribute("name", "dalaoyang");
    return "index";
  }

}
